package CGW.controller;



import CGW.bean.Dinner_table;
import CGW.bean.Page;
import CGW.bean.User;
import CGW.dao.BaseDao;
import CGW.dao.DingdanDao;
import CGW.dao.UserDao;
import CGW.service.DingdanService;
import CGW.service.UserService;
import CGW.utils.ZHI;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/users")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserService service = new UserService(new UserDao());
//        Dinner_tableService service1=new Dinner_tableService(new Dinner_tableDao());
        String action = req.getParameter("action");
        if(action == null) {
//            req.getSession().invalidate(); // 清除session数据
            String page = req.getParameter("page");
            int pageInt = 1;
            String pageSize = req.getParameter("pageSize");
            int pageSizeInt = 20;
            if(page!=null){
                pageInt = Integer.parseInt(page);
            }
            if(pageSize !=null){
                pageSizeInt = Integer.parseInt(pageSize);
            }
            Page<User> pageBean= service.queryAll(pageInt, pageSizeInt);
            req.setAttribute("pageBean", pageBean);
            req.getRequestDispatcher("users.jsp").forward(req, resp);
        }else if(action.equals("update")){
            String user_id = req.getParameter("user_id");
            // 依据user_id 获取用户的信息
            User user = service.get(Integer.parseInt(user_id));
            req.setAttribute("user", user);
            req.getRequestDispatcher("user_update.jsp").forward(req, resp);
        }else if(action.equals("update1")){
            String user_id = req.getParameter("user_id");
            // 依据user_id 获取用户的信息
            User user = service.get(Integer.parseInt(user_id));
            req.setAttribute("user", user);
            req.getRequestDispatcher("yue.jsp").forward(req, resp);
        }else if(action.equals("delete")){
            String user_id = req.getParameter("user_id");
            boolean ret = service.delete(Integer.parseInt(user_id));
            if(ret){
                req.getSession().setAttribute("delMsg", "删除成功");
            }else{
                req.getSession().setAttribute("delMsg", "删除失败");
            }
            resp.sendRedirect("/users");
        }else if(action.equals("add")){
            service.close();
            User user=new User();
            req.setAttribute("user", user);
            req.getRequestDispatcher("user_update.jsp").forward(req, resp);
        }else if(action.equals("delMsg")){
            // 异步请求及响应
            req.getSession().removeAttribute("delMsg");
            resp.getWriter().println("ok");
        }else if (action.equals("jiezhang")) {
            BaseDao<User> dao = new BaseDao<>();
            User login_user = (User) req.getSession().getAttribute("login_user");
            Double bal = login_user.getBalance();

            String balance = req.getParameter("order_total_price");
            String user_id = req.getParameter("user_id");
            String table_id = req.getParameter("table_id");
            System.out.println(user_id);
            System.out.println(table_id);
            Double balance1 = bal - Double.parseDouble(balance);
            User user = new User(ZHI.zh(user_id), balance1);
            int a = dao.update_one_user_balance(user);
            dao.commit();
            if (a > 0) {

                Dinner_table table = new Dinner_table(ZHI.zh(table_id), 0);
                int i = dao.update_one(table);
                dao.commit();
                if (i > 0) {
                    System.out.println("更改状态成功");
                }

                System.out.println("更新成功");
                DingdanService service1 = new DingdanService(new DingdanDao());
                String order_id = req.getParameter("order_id");
                boolean c = service1.delete(ZHI.zh(order_id));
                if (c) {
                    System.out.println("删除成功");
                }
            } else {
                System.out.println("更新失败");
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserService service = new UserService(new UserDao());

        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String nick_name = req.getParameter("nick_name");
        String is_admin = req.getParameter("is_admin");
        String phone = req.getParameter("phone");
        String gender = req.getParameter("gender");
        String user_status = req.getParameter("user_status");
        String user_create_time = req.getParameter("user_create_time");
        String user_update_time = req.getParameter("user_update_time");
        String is_delete = req.getParameter("is_delete");
        String is_member = req.getParameter("is_member");
        String balance = req.getParameter("balance");
        String user_id = req.getParameter("user_id");
        System.out.println(user_id);

        boolean ret = false;
        User user = null;
        if(user_id.equals("")){
            // 新增
            user = new User(username,password);
            ret = service.insert(user);
            resp.sendRedirect("/login");
        } else if (is_admin.equals("0")) {
            User a=(User) req.getSession().getAttribute("login_user");
            double balance1 = a.getBalance();
            user = new User(ZHI.zh(user_id),username,password,nick_name,ZHI.zh(is_admin),phone,ZHI.zh(gender),ZHI.zh(user_status),ZHI.dt(user_create_time),ZHI.dt(user_update_time),ZHI.zh(is_delete),ZHI.zh(is_member),Double.valueOf(Double.parseDouble(balance))+balance1);
            ret = service.update(user);
            if(ret){
                req.getSession().setAttribute("login_user", user);
                resp.sendRedirect("/userlook1");
            }else{
                req.setAttribute("user", user);
                req.setAttribute("error", user_id.equals("0")?"新增失败":"更新失败");
                req.getRequestDispatcher("/user_update.jsp").forward(req, resp);
            }
        } else{
            user = new User(ZHI.zh(user_id),username,password,nick_name,ZHI.zh(is_admin),phone,ZHI.zh(gender),ZHI.zh(user_status),ZHI.dt(user_create_time),ZHI.dt(user_update_time),ZHI.zh(is_delete),ZHI.zh(is_member),Double.valueOf(Double.parseDouble(balance)));
            ret = service.update(user);
            if(ret){
                resp.sendRedirect("/users");
            }else{
                req.setAttribute("user", user);
                req.setAttribute("error", user_id.equals("0")?"新增失败":"更新失败");
                req.getRequestDispatcher("/user_update.jsp").forward(req, resp);
            }
        }

    }
}
